package com.lenovo.manufacture.module;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.BootstrapButton;
import com.beardedhen.androidbootstrap.BootstrapEditText;
import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.annotation.Layout;

import butterknife.BindView;
import butterknife.OnClick;

public class Module6 extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.tv_gold)
    TextView tvGold;
    @BindView(R.id.et_pos)
    BootstrapEditText etPos;
    @BindView(R.id.et_id)
    BootstrapEditText etId;
    @BindView(R.id.btn_buy)
    BootstrapButton btnBuy;

    @Layout(value = R.layout.activity_module6, title = "购买原材料")
    protected void init() {
        async(apiService.getInfo(param.add("id", 1).build()), data -> {
            String gold = data.getData().get(0).get("gold");
            tvGold.setText(String.format("工厂资金：%s", gold));
        });
    }

    @OnClick(R.id.btn_buy)
    public void onClick(View v) {
        String pos = etPos.getText().toString();
        if (TextUtils.isEmpty(pos)) {
            showMsg("位置不能为空");
            return;
        }

        String id = etId.getText().toString().trim();
        if (TextUtils.isEmpty(id)) {
            showMsg("供应原料ID不能为空");
            return;
        }

        try {
            Integer integer = Integer.valueOf(pos);
            Integer supplyListId = Integer.valueOf(id);
            if (integer <= 4 && integer >= 1) {
                //根据位置获取生产线id
                async(apiService.userLineSearch(param.add("pos", integer - 1).build()), data -> {
                    String lineId = data.getData().get(0).get("lineId");
                    async(apiService.addMaterial(param.add("userLineId", lineId).add("num", 1).add("supplyListId", supplyListId).build()), data1 -> {
                        showMsg("购买成功");
                        init();
                    });
                });
            } else {
                showMsg("请输入正确的位置（1，2，3，4）");
            }
        } catch (Exception e) {
            showMsg("请输入正确的位置与供应原料ID");
        }
    }
}
